﻿#include "user_reservation.h"
#include "data_structure.h"
#include "facility.h"
#include "facility_manager.h"


//清理缓存区
void clear_input_buffer3() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF); // 读取到换行符或文件尾
}

//解析字符串到结构体
//判断时间输入格式
//int parse_time_string(char* input, Res_node* res_node) {
//    // 临时变量存储解析结果
//    char start_hour[3], end_hour[3];
//
//    // 使用sscanf解析带中文的时间字符串
//    int result = sscanf(input, "%4s年%2s月%2s日%2s-%2s点",
//        res_node->user_res.year, res_node->user_res.month, res_node->user_res.day,
//        start_hour, end_hour);
//
//
//    // 合并时间区间
//    if (result == 5) {
//        strncpy_s(res_node->user_res.start_time,3, start_hour,3);
//        strncpy_s(res_node->user_res.end_time,3, end_hour,3);
//        
//        return 1;         // 解析成功
//    }
//    return 0;             // 解析失败
//}
//打印时间函数
// 
//
int parse_time_string(char* input, Res_node* res_node) {
    // 临时变量存储解析结果
    char start_hour[6], end_hour[6];

    // 使用sscanf解析带中文的时间字符串
    int result = sscanf(input, "%4s年%2s月%2s日%5s-%5s",
        res_node->user_res.year, res_node->user_res.month, res_node->user_res.day,
        start_hour, end_hour);

    // 合并时间区间
    if (result == 5) {
        strncpy_s(res_node->user_res.start_time, 6, start_hour, 5);
        res_node->user_res.start_time[5] = '\0';
        strncpy_s(res_node->user_res.end_time, 6, end_hour, 5);
        res_node->user_res.end_time[5] = '\0';

        return 1;         // 解析成功
    }
    return 0;             // 解析失败

}
//时间解析函数（2025年04月19日08:30-10:30）

//创建链表节点
Res_node* create_res_node() {
    Res_node* node = malloc(sizeof(Res_node));
    if (node) {
        memset(&node->user_res, 0, sizeof(User_res)); // 清零初始化
        node->next = NULL;
    }
    return node;
}

//链表初始化
void init_res_list(Res_list* res_list) {
    res_list->head = NULL;
    res_list->size = 0;
    res_list->id = 0;
}

//加入链表
void append_res_list(Res_list* res_list, Res_node* res_node) {
    if (res_list == NULL || res_node == NULL) {
        printf("参数错误！\n");
        return;
    }


    if (res_list->head == NULL) {
        res_list->head = res_node;
    }
    else {
        Res_node* cur_node = res_list->head;
        while (cur_node->next != NULL) {
            cur_node = cur_node->next;
        }
        cur_node->next = res_node;

    }
    res_list->size++;
    res_list->id++;
}

//清空当前链表
void free_res_list(Res_list* res_list) {
    Res_node* cur_node = res_list->head;
    Res_node* q_node = NULL;
    while (cur_node != NULL) {
        q_node = cur_node->next;
        free(cur_node);
        cur_node = q_node;
        res_list->size--;
        /*printf("已清一个\n");*/
    }
    res_list->head = NULL;
}

//打印当前全部链表
void printf_res(Res_list res_list) {

    Res_node* cur_node = res_list.head;
    if (cur_node)
        while (cur_node != NULL) {
            printf("%s\t%s\t%s\t%d\t%d\t", 
                cur_node->user_res.name, 
                cur_node->user_res.id_name, 
                cur_node->user_res.phone, 
                cur_node->user_res.venue_Type,
                cur_node->user_res.num);

            //打印时间字段
            printf("%s/", cur_node->user_res.year);
            printf("%s/", cur_node->user_res.month);
            printf("%s/", cur_node->user_res.day);
            printf("%s-", cur_node->user_res.start_time);
            printf("%s\t", cur_node->user_res.end_time);
            //打印状态
            printf("到场状态：%d\t",cur_node->user_res.flag);
            //打印该预约信息编号
            printf("该预约信息的编号为：%d\n", cur_node->user_res.res_count);

            cur_node = cur_node->next;
        }
}

// 打印一个结点的预约信息
void print_res(Res_node* res_node) {
    /*printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");*/
    printf("姓名：%s 身份证号：%s 电话：%s ",
        res_node->user_res.name,
        res_node->user_res.id_name,
        res_node->user_res.phone);
    printf("场地类型：");
    print_facility_type(res_node->user_res.venue_Type);
    //print_facility_type(res_node->user_res.venue_Type);
    //打印时间字段
    printf(" 时间：");
    printf("%s/", res_node->user_res.year);
    printf("%s/", res_node->user_res.month);
    printf("%s/", res_node->user_res.day);
    printf("%s-", res_node->user_res.start_time);
    printf("%s ", res_node->user_res.end_time);
    //打印状态
    printf("到场状态：%d ", res_node->user_res.flag);
    //打印该预约信息编号
    printf("编号：%d\n", res_node->user_res.res_count);

}

//打印当前用户链表
void printf_cur_res(Res_list res_list, User_node* cur_user_node) {
    Res_node* cur_node = res_list.head;
    int flag = 0;
    while (cur_node != NULL) {
        if (cur_node && !strcmp(cur_node->user_res.phone, cur_user_node->user.phone)) {
                flag = 1;
                printf("%s\t%s\t%s\t%d\t%d\t",
                    cur_node->user_res.name,
                    cur_node->user_res.id_name,
                    cur_node->user_res.phone,
                    cur_node->user_res.venue_Type,
                    cur_node->user_res.num);

                //打印时间字段
                printf("%s/", cur_node->user_res.year);
                printf("%s/", cur_node->user_res.month);
                printf("%s/", cur_node->user_res.day);
                printf("%s-", cur_node->user_res.start_time);
                printf("%s\t", cur_node->user_res.end_time);
                //打印状态
                printf("到场状态：%d\t", cur_node->user_res.flag);
                //打印该预约信息编号
                printf("该预约信息的编号为：%d\n",cur_node->user_res.res_count);
        }       
        cur_node = cur_node->next;    
    }
    if (flag == 0) {
        printf("当前未能找到您的信息！\n");
    }
    
}

//保存预约信息至文件中
void save_res_data(Res_list* res_list) {

    //打开文件
    FILE* fp = fopen("..\\src\\res_data.txt", "w");
    if (!fp) {
        perror("file open failed");
        return;
    }

    //写入用户信息
    Res_node* cur_node = res_list->head;
    while (cur_node != NULL) {


        fprintf(fp, "%s\t%s\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%d\n", 
            cur_node->user_res.name, 
            cur_node->user_res.phone, 
            cur_node->user_res.id_name,
            cur_node->user_res.venue_Type,
            cur_node->user_res.num,
            cur_node->user_res.year, 
            cur_node->user_res.month,
            cur_node->user_res.day,
            cur_node->user_res.start_time,
            cur_node->user_res.end_time,
            cur_node->user_res.flag,
            cur_node->user_res.res_count);
        cur_node = cur_node->next;
    }

    //关闭文件
    fclose(fp);

}

//读取预约信息至链表中
void read_res_data(Res_list* user_list) {

    //打开文件
    FILE* fp = fopen("..\\src\\res_data.txt", "r");
    if (!fp) {
        perror("file open failed");
        return;
    }


    while (true) {

        Res_node* res_node = create_res_node();
        int read = fscanf_s(fp, "%s\t%s\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%d\n",
            res_node->user_res.name,50,
            res_node->user_res.phone,50,
            res_node->user_res.id_name,50,
            &res_node->user_res.venue_Type,
            &res_node->user_res.num,
            res_node->user_res.year,5,
            res_node->user_res.month,3,
            res_node->user_res.day,3,
            res_node->user_res.start_time,6,
            res_node->user_res.end_time,6,
            &res_node->user_res.flag,
            &res_node->user_res.res_count);
        //if (read == 0) {
        //    free(res_node);
        //    break;
        //}

        //append_res_list(user_list, res_node);
        if (read == 12) {
            append_res_list(user_list, res_node);
        }
        else {
            free(res_node);
            break;
        }
    }


    //关闭文件
    fclose(fp);

}

//根据编号获取预约信息
User_res* find_res(Res_list* res_list, int id) {
    Res_node* cur_node = res_list->head;
    int flag = 0;
    while (cur_node != NULL) {
        if (cur_node && cur_node->user_res.res_count == id) {
            return &cur_node->user_res;
        }
        cur_node = cur_node->next;
    }
    if (flag == 0) {
        printf("当前未能找到您的信息！\n");
        return NULL;
    }
}

//场地类型包括
//篮球场，羽毛球场，足球场，台球场，乒乓球场，排球场

//预约函数(修改前）
/*
void res_system(User_node* cur_user_node,System* sys) {
    Res_node* res_node = create_res_node();
    res_node->user_res.flag = 0;
    res_node->user_res.num = 0;
    
    char input[50];
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("*           请选择您的预约信息            *\n");
    printf("*                1,篮球场                 *\n");
    printf("*                2,羽毛球场               *\n");
    printf("*                3,足球场                 *\n");
    printf("*                4,台球场                 *\n");
    printf("*                5,乒乓球场               *\n");
    printf("*                6,排球场                 *\n");
    printf("*******************************************\n");
    printf("select->");
    //选择菜单
    scanf_s("%d", &res_node->user_res.venue_Type);
    clear_input_buffer3();
    // 打印场地信息
    print_facility_by_type(res_node->user_res.venue_Type,&sys->facility_link);
    //选择场地编号
    printf("select->(如果不选择请输入0！)");
    scanf_s("%d",&res_node->user_res.num);
    clear_input_buffer3();
    
    if (res_node->user_res.num != 0) {
        Facility* facility = find_facility(&sys->facility_link, res_node->user_res.num);
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*            正在为您预约                 *\n");
        printf("*******************************************\n");
        int res_year = 0;
        int res_month = 0;
        int res_day = 0;
        printf("可预约日期：");
        // 获取当日时间
        time_t now = 0;
        time(&now);
        struct tm to_tm = *localtime(&now);
        // 输出可预约的日期
        to_tm.tm_mday += 1;
        mktime(&to_tm);
        printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
        to_tm.tm_mday += 6;
        mktime(&to_tm);
        printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
        while (true) {
            system("cls");
            printf("*******************************************\n");
            printf("*            正在为您预约                 *\n");
            printf("*******************************************\n");
            printf("可预约日期：");
            // 获取当日时间
            time_t now = 0;
            time(&now);
            struct tm to_tm = *localtime(&now);
            // 输出可预约的日期
            to_tm.tm_mday += 1;
            mktime(&to_tm);
            printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
            to_tm.tm_mday += 6;
            mktime(&to_tm);
            printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
            // 选择日期
            printf("请输入年份: ");
            scanf_s("%d", &res_year);
            clear_input_buffer3();
            printf("请输入月份: ");
            scanf_s("%d", &res_month);
            clear_input_buffer3();
            printf("请输入日期: ");
            scanf_s("%d", &res_day);
            clear_input_buffer3();
            //场地人流输出
            if (print_time_list(*facility, res_year, res_month, res_day)) {
                break;
            }
        }
    }
    else {
        // 获取当日时间
        time_t now = 0;
        time(&now);
        struct tm to_tm = *localtime(&now);
        // 输出可预约的日期
        to_tm.tm_mday += 1;
        mktime(&to_tm);
        printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
        to_tm.tm_mday += 6;
        mktime(&to_tm);
        printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
    }
    //函数（res_year，res_month，res_day）
    while (true) {
        printf("请输入日期信息(示例：2021年03月18日08:00-10:00)\n> ");
        fgets(input, sizeof(input), stdin);
        input[strcspn(input, "\n")] = 0;
        if (parse_time_string(input, res_node)) {
            char start_hour[6], end_hour[6];
            strncpy_s(start_hour, 6, res_node->user_res.start_time, 5);
            start_hour[5] = '\0';
            strncpy_s(end_hour, 6, res_node->user_res.end_time, 5);
            end_hour[5] = '\0';
            if (judge_time(sys, start_hour, end_hour, res_node->user_res.num, res_node->user_res.year, res_node->user_res.month, res_node->user_res.day)) {
                strncpy_s(res_node->user_res.name, 50, cur_user_node->user.name, 50);
                strncpy_s(res_node->user_res.id_name, 50, cur_user_node->user.id_name, 50);
                strncpy_s(res_node->user_res.phone, 50, cur_user_node->user.phone, 50);
                res_node->user_res.res_count = sys->reservation_link.id + 1;
                append_res_list(&sys->reservation_link, res_node);
                save_res_data(&sys->reservation_link);
                printf("信息已成功保存\n");
                break;
            }
            else {
                continue;
            }
        }
        else {
            printf("输入格式错误！请按示例格式输入！\n");
        }
    }



}*/

//修改当前账号预约信息函数
void change_cur_res(System* sys, User_node* cur_user_node) {
    int temp_id = 0;
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("请输入当前要修改的预约信息编号： \n>");
    scanf_s("%d", &temp_id);
    clear_input_buffer3();
    Res_node* cur_node = sys->reservation_link.head;
    int flag_cx = 0;
    while (cur_node != NULL) {
        if (temp_id == cur_node->user_res.res_count && !strcmp(cur_node->user_res.phone, cur_user_node->user.phone)) {
            flag_cx = 1;//已查找到对应账号下的预约编号信息
            system("pause");
            system("cls");
            printf("*******************************************\n");
            printf("*           欢迎使用运动场馆系统          *\n");
            printf("*******************************************\n");
            printf("当前账号预约编号下的场地类型，场地编号，时间为；\n");
            printf("%d\t%d\t",
                cur_node->user_res.venue_Type,
                cur_node->user_res.num);
            printf("%s/", cur_node->user_res.year);
            printf("%s/", cur_node->user_res.month);
            printf("%s/", cur_node->user_res.day);
            printf("%s-", cur_node->user_res.start_time);
            printf("%s\n", cur_node->user_res.end_time);
            //修改部分
            int flag_pd = 1;
            while (flag_pd) {
                int select = -1;
                while (true) {
                    system("pause");
                    system("cls");
                    printf("*******************************************\n");
                    printf("*           欢迎使用运动场馆系统          *\n");
                    printf("*******************************************\n");
                    printf("           请选择你想要修改的选项         *\n");
                    printf("*                1,场地类型               *\n");
                    printf("*                2,场地编号               *\n");
                    printf("*                3,预约时间               *\n");
                    printf("*                0,退出修改               *\n");
                    printf("*******************************************\n");
                    printf("select->");
                    scanf_s("%d", &select);
                    clear_input_buffer();//输入字符型会被清缓存区的函数清理
                    if (select != 1 && select != 2 && select != 3 && select != 0) {
                        printf("当前输入的修改选项有误，请重新输入！\n");
                        continue;
                    }
                    else {
                        break;
                    }
                }

                char input[50];
                switch (select) {
                case 1:
                    //输入场地类型（如果选择场地类型，需要重新输入场地编号）
                    ;
                    int temp_type = cur_node->user_res.venue_Type;
                    while (true) {
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*           欢迎使用运动场馆系统          *\n");
                        printf("*******************************************\n");
                        printf("*           请修改您的预约信息            *\n");
                        printf("*                1,篮球场                 *\n");
                        printf("*                2,羽毛球场               *\n");
                        printf("*                3,足球场                 *\n");
                        printf("*                4,台球场                 *\n");
                        printf("*                5,乒乓球场               *\n");
                        printf("*                6,排球场                 *\n");
                        printf("*                0,返回                   *\n");
                        printf("*******************************************\n");
                        cur_node->user_res.venue_Type = -1;
                        scanf_s("%d", &cur_node->user_res.venue_Type);
                        clear_input_buffer();
                        if (cur_node->user_res.venue_Type == -1 ||
                            cur_node->user_res.venue_Type == 7 ||
                            cur_node->user_res.venue_Type == 8 ||
                            cur_node->user_res.venue_Type == 9
                            ) {
                            cur_node->user_res.venue_Type = temp_type;
                            printf("当前输入的场地类型有误，请重新输入！\n");
                            continue;
                        }
                        if (cur_node->user_res.venue_Type == 0) {
                            cur_node->user_res.venue_Type = temp_type;
                            printf("正在返回修改预约信息界面！          \n");
                            break;
                        }
                        else {
                            int temp_num = cur_node->user_res.num;
                            while (true) {
                                system("pause");
                                system("cls");
                                printf("*******************************************\n");
                                printf("*            正在为您修改                 *\n");
                                printf("*******************************************\n");
                                printf("当前的场地的场地编号有(可以输入0，视作为不选)：\n");
                                print_facility_by_type(cur_node->user_res.venue_Type, &sys->facility_link);
                                printf("select->(如果不选择请输入0！)");
                                cur_node->user_res.num = -1;
                                scanf_s("%d", &cur_node->user_res.num);
                                clear_input_buffer3();
                                Facility* facility = find_facility(&sys->facility_link, cur_node->user_res.num);
                                if (cur_node->user_res.num == -1) {
                                    printf("当前输入的场地编号有误，请重新输入！\n");
                                    continue;
                                }
                                else if (cur_node->user_res.num == 0 || facility) {
                                    printf("已成功修改场地编号！\n");
                                    break;
                                }
                                else {
                                    printf("未找到输入的场地编号,请重新输入！\n");
                                    continue;
                                }

                            }
                            break;
                        }

                    }
                    break;
                case 2:
                    //输入场地编号
                    ;
                    int temp_num = cur_node->user_res.num;
                    while (true) {
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您修改                 *\n");
                        printf("*******************************************\n");
                        printf("当前的场地的场地编号有(可以输入0，视作为不选)：\n");
                        print_facility_by_type(cur_node->user_res.venue_Type, &sys->facility_link);
                        printf("select->(返回输入-2)");
                        cur_node->user_res.num = -1;
                        scanf_s("%d", &cur_node->user_res.num);
                        clear_input_buffer3();
                        Facility* facility = find_facility(&sys->facility_link, cur_node->user_res.num);
                        if (cur_node->user_res.num == -2) {
                            cur_node->user_res.num = temp_num;
                            break;
                        }
                        else if (cur_node->user_res.num == -1) {
                            printf("当前输入的场地编号有误，请重新输入！\n");
                            continue;
                        }
                        else if (cur_node->user_res.num == 0 || facility) {
                            printf("已成功修改场地编号！\n");
                            break;
                        }
                        else {
                            printf("未找到输入的场地编号,请重新输入！\n");
                            continue;
                        }

                    }
                    break;
                case 3:
                    //输入预约时间
                    if (cur_node->user_res.num != 0) {
                        Facility* facility = find_facility(&sys->facility_link, cur_node->user_res.num);
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您预约新日期           *\n");
                        printf("*******************************************\n");
                        int res_year = 0;
                        int res_month = 0;
                        int res_day = 0;
                        printf("可预约日期：");
                        // 获取当日时间
                        time_t now = 0;
                        time(&now);
                        struct tm to_tm = *localtime(&now);
                        // 输出可预约的日期
                        to_tm.tm_mday += 1;
                        mktime(&to_tm);
                        printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                        to_tm.tm_mday += 6;
                        mktime(&to_tm);
                        printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                        while (true) {
                            system("pause");
                            system("cls");
                            printf("*******************************************\n");
                            printf("*            正在为您预约新日期           *\n");
                            printf("*******************************************\n");
                            printf("可预约日期：");
                            // 获取当日时间
                            time_t now = 0;
                            time(&now);
                            struct tm to_tm = *localtime(&now);
                            // 输出可预约的日期
                            to_tm.tm_mday += 1;
                            mktime(&to_tm);
                            printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                            to_tm.tm_mday += 6;
                            mktime(&to_tm);
                            printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                            // 选择日期
                            printf("请输入年份: ");
                            scanf_s("%d", &res_year);
                            clear_input_buffer3();
                            printf("请输入月份: ");
                            scanf_s("%d", &res_month);
                            clear_input_buffer3();
                            printf("请输入日期: ");
                            scanf_s("%d", &res_day);
                            clear_input_buffer3();
                            //场地人流输出
                            if (print_time_list(*facility, res_year, res_month, res_day)) {
                                break;
                            }
                        }
                    }
                    else {
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您预约新日期           *\n");
                        printf("*******************************************\n");
                        printf("可预约日期：");
                        // 获取当日时间
                        time_t now = 0;
                        time(&now);
                        struct tm to_tm = *localtime(&now);
                        // 输出可预约的日期
                        to_tm.tm_mday += 1;
                        mktime(&to_tm);
                        printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                        to_tm.tm_mday += 6;
                        mktime(&to_tm);
                        printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                    }
                    //函数（res_year，res_month，res_day）
                    while (true) {
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您预约新日期           *\n");
                        printf("*******************************************\n");
                        printf("请输入日期信息(示例：2021年03月18日08:00-10:00)\n> ");
                        fgets(input, sizeof(input), stdin);
                        input[strcspn(input, "\n")] = 0;
                        if (parse_time_string(input, cur_node)) {
                            char start_hour[6], end_hour[6];
                            strncpy_s(start_hour, 6, cur_node->user_res.start_time, 5);
                            start_hour[5] = '\0';
                            strncpy_s(end_hour, 6, cur_node->user_res.end_time, 5);
                            end_hour[5] = '\0';
                            if (judge_time(sys, start_hour, end_hour, cur_node->user_res.num, cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day)) {
                                printf("已成功为您修改预约日期！\n");
                                break;
                            }
                            else {
                                continue;
                            }
                        }
                        else {
                            printf("输入格式错误！请按示例格式输入！\n");
                        }
                    }
                    break;
                case 0:
                    flag_pd = 0;
                    system("pause");
                    system("cls");
                    printf("*******************************************\n");
                    printf("*             正在返回功能界面            *\n");
                    printf("*******************************************\n");
                    break;
                }
            }
        }
        if (flag_cx == 1) {
            break;
        }
        cur_node = cur_node->next;
    }
    if (flag_cx == 0) {
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*           欢迎使用运动场馆系统          *\n");
        printf("*******************************************\n");
        printf("*未在当前账号下找到输入的预约编号！       *\n");
    }
}

//删除某一条预约信息
void delete_cur_res(Res_list* res_list, User_node* cur_user_node) {
    int temp_id = -1;
    while (true) {
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*           欢迎使用运动场馆系统          *\n");
        printf("*******************************************\n");
        printf("请输入当前要删除的预约信息编号： \n>");
        scanf_s("%d", &temp_id);
        clear_input_buffer3();
        if (temp_id == -1) {
            printf("当前输入的预约信息编号有误，请重新输入！\n");
            continue;
        }
        else {
            break;
        }
    }

    Res_node* cur_node = res_list->head;
    Res_node* prev_node = NULL;  // 新增前驱节点指针
    int flag_cx2 = 0;

    while (cur_node != NULL) {
        if (temp_id == cur_node->user_res.res_count &&
            !strcmp(cur_node->user_res.phone, cur_user_node->user.phone))
        {
            flag_cx2 = 1;
            if (prev_node == NULL) {
                // 删除的是头节点
                res_list->head = cur_node->next;
            }
            else {
                // 删除中间或尾节点
                prev_node->next = cur_node->next;
            }

            free(cur_node);  // 释放节点内存
            printf("* 预约编号 %d 已成功删除！              *\n", temp_id);
            break;  // 删除后退出循环
            

        }
        else {
            // 未匹配时继续遍历
            prev_node = cur_node;        // 保存前驱节点
            cur_node = cur_node->next;   // 移动到下一个节点
        }
    }
    if (flag_cx2 == 0) {
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*           欢迎使用运动场馆系统          *\n");
        printf("*******************************************\n");
        printf("*未在当前账号下找到输入的预约编号！       *\n");
    }
}

// 读取预约消息
void read_res_set_facility(User_res user_res, System* sys) {
    int year = atoi(user_res.year);
    int month = atoi(user_res.month);
    int day = atoi(user_res.day);
    char start_time[6], end_time[6];
    strncpy_s(start_time,6, user_res.start_time, 5);
    start_time[5] = '\0';
    strncpy_s(end_time,6, user_res.end_time, 5);
    end_time[5] = '\0';
    int start = index_time(start_time);
    int end = index_time(end_time);
    Facility* facility = find_facility(&sys->facility_link, user_res.num);
    if (facility != NULL) {
        TNode* t_node = facility->time_list.head->next;
        while (t_node != NULL) {
            if (t_node->t.year == year && t_node->t.month == month && t_node->t.day == day) {
                for (int i = start; i < end; i++) {
                    t_node->t.day_time[i] = t_node->t.day_time[i] + 1;
                }
                break;
            }
            else {
                t_node = t_node->next;
            }
        }
    }
    
}

// 打印仅预约场地
void print_only_type(Res_list* res_list) {
    Res_node* cur_node = res_list->head->next;
    while (cur_node != NULL) {
        if (cur_node->user_res.flag == false && cur_node->user_res.num == 0) {
            printf("%s\t%s\t%s\t%d\t%d\t",
                cur_node->user_res.name,
                cur_node->user_res.id_name,
                cur_node->user_res.phone,
                cur_node->user_res.venue_Type,
                cur_node->user_res.num);

            //打印时间字段
            printf("%s/", cur_node->user_res.year);
            printf("%s/", cur_node->user_res.month);
            printf("%s/", cur_node->user_res.day);
            printf("%s-", cur_node->user_res.start_time);
            printf("%s\t", cur_node->user_res.end_time);
            ////打印状态
            //printf("到场状态：%d\t", cur_node->user_res.flag);
            //打印该预约信息编号
            printf("该预约信息的编号为：%d\n", cur_node->user_res.res_count);
        }
        cur_node = cur_node->next;
    }
}

// 查找预订信息 姓名 模糊
void search_reservation_by_name(System sys) {
    char search_name[50] = {'\0'};
    int cnt = 0;
    printf(" 输入要查询的姓名：");
    scanf_s("%s", search_name, 50);
    clear_input_buffer();
    /*printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");*/
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strstr(cur_node->user_res.name, search_name) != NULL) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);

}
// 查找预订信息 电话
void search_reservation_by_phone(System sys) {
    char search_phone[50] = { '\0' };
    int cnt = 0;
    printf(" 输入要查询的电话：");
    scanf_s("%s", search_phone, 50);
    clear_input_buffer();
    printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.phone,search_phone)==0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}
// 查找预订信息 日期
void search_reservation_by_date(System sys) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    int cnt = 0;
    // 健壮性问题！
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日",year,5,month,3,day,3);
    clear_input_buffer();
    printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.year, year) == 0&&
            strcmp(cur_node->user_res.month, month) == 0&&
            strcmp(cur_node->user_res.day, day) == 0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}
// 查找预订信息 姓名+日期
void search_reservation_by_name_date(System sys) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    char search_name[50] = { '\0' };
    int cnt = 0;
    // 健壮性问题！
    printf(" 输入要查询的姓名：");
    scanf_s("%s", search_name, 50);
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.name,search_name)==0 &&
            strcmp(cur_node->user_res.year, year) == 0 &&
            strcmp(cur_node->user_res.month, month) == 0 &&
            strcmp(cur_node->user_res.day, day) == 0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}
// 查找预订信息 电话+日期
void search_reservation_by_phone_date(System sys) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    char phone[50] = { '\0' };
    int cnt = 0;
    // 健壮性问题！
    printf(" 输入要查询的电话：");
    scanf_s("%s", phone, 50);
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    printf("姓名 \t 身份证号\t 电话\t 场地类型\t 预约ID\t 时间\n");
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.phone, phone) == 0 &&
            strcmp(cur_node->user_res.year, year) == 0 &&
            strcmp(cur_node->user_res.month, month) == 0 &&
            strcmp(cur_node->user_res.day, day) == 0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}


//预约函数
void res_system(User_node* cur_user_node, System* sys) {

    char input[50];

    while (true) {
        Res_node* res_node = create_res_node();
        res_node->user_res.flag = 0;
        res_node->user_res.num = 0;
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*           欢迎使用运动场馆系统          *\n");
        printf("*******************************************\n");
        printf("*           请选择您的预约信息            *\n");
        printf("*                1,篮球场                 *\n");
        printf("*                2,羽毛球场               *\n");
        printf("*                3,足球场                 *\n");
        printf("*                4,台球场                 *\n");
        printf("*                5,乒乓球场               *\n");
        printf("*                6,排球场                 *\n");
        printf("*                0,返回                   *\n");
        printf("*******************************************\n");
        printf("select->");
        res_node->user_res.venue_Type = -1;
        scanf_s("%d", &res_node->user_res.venue_Type);
        clear_input_buffer3();
        if (res_node->user_res.venue_Type == -1||
            res_node->user_res.venue_Type == 7||
            res_node->user_res.venue_Type == 8||
            res_node->user_res.venue_Type == 9
            ) {
            free(res_node);
            res_node = NULL;
            printf("当前输入的场地类型有误，请重新输入！\n");
            continue;
        }
        if (res_node->user_res.venue_Type == 0) {
            free(res_node);
            res_node = NULL;
            break;
        }
        else {
            while (true) {
                system("pause");
                system("cls");
                printf("*******************************************\n");
                printf("*            正在为您预约                 *\n");
                printf("*******************************************\n");
                printf("当前的场地的场地编号有(可以输入0，视作为不选)：\n");
                print_facility_by_type(res_node->user_res.venue_Type, &sys->facility_link);
                printf("select->(如果不选择请输入0！)");
                res_node->user_res.num = -1;
                scanf_s("%d", &res_node->user_res.num);
                clear_input_buffer3();
                Facility* facility = find_facility(&sys->facility_link, res_node->user_res.num);
                if (res_node->user_res.num == -1) {
                    printf("当前输入的场地编号有误，请重新输入！\n");
                    continue;
                }
                else if (res_node->user_res.num == 0) {
                    system("pause");
                    system("cls");
                    printf("*******************************************\n");
                    printf("*            正在为您预约                 *\n");
                    printf("*******************************************\n");
                    printf("可预约日期：");
                    // 获取当日时间
                    time_t now = 0;
                    time(&now);
                    struct tm to_tm = *localtime(&now);
                    // 输出可预约的日期
                    to_tm.tm_mday += 1;
                    mktime(&to_tm);
                    printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                    to_tm.tm_mday += 6;
                    mktime(&to_tm);
                    printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                    while (true) {
                        system("pause");
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您预约                 *\n");
                        printf("*******************************************\n");
                        printf("请输入日期信息(示例：2021年03月18日08:00-10:00)\n> ");
                        fgets(input, sizeof(input), stdin);
                        input[strcspn(input, "\n")] = 0;
                        if (parse_time_string(input, res_node)) {
                            char start_hour[6], end_hour[6];
                            strncpy_s(start_hour, 6, res_node->user_res.start_time, 5);
                            start_hour[5] = '\0';
                            strncpy_s(end_hour, 6, res_node->user_res.end_time, 5);
                            end_hour[5] = '\0';
                            if (judge_time(sys, start_hour, end_hour, res_node->user_res.num, res_node->user_res.year, res_node->user_res.month, res_node->user_res.day)) {
                                strncpy_s(res_node->user_res.name, 50, cur_user_node->user.name, 50);
                                strncpy_s(res_node->user_res.id_name, 50, cur_user_node->user.id_name, 50);
                                strncpy_s(res_node->user_res.phone, 50, cur_user_node->user.phone, 50);
                                res_node->user_res.res_count = sys->reservation_link.id + 1;
                                append_res_list(&sys->reservation_link, res_node);
                                save_res_data(&sys->reservation_link);
                                printf("信息已成功保存\n");
                                break;
                            }
                            else {
                                continue;
                            }
                        }
                        else {
                            printf("输入格式错误！请按示例格式输入！\n");
                        }
                    }
                    break;
                }
                else if (facility != NULL) {
                    system("pause");
                    system("cls");
                    printf("*******************************************\n");
                    printf("*            正在为您预约                 *\n");
                    printf("*******************************************\n");
                    int res_year = 0;
                    int res_month = 0;
                    int res_day = 0;
                    printf("可预约日期：");
                    // 获取当日时间
                    time_t now = 0;
                    time(&now);
                    struct tm to_tm = *localtime(&now);
                    // 输出可预约的日期
                    to_tm.tm_mday += 1;
                    mktime(&to_tm);
                    printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                    to_tm.tm_mday += 6;
                    mktime(&to_tm);
                    printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                    while (true) {
                        system("cls");
                        printf("*******************************************\n");
                        printf("*            正在为您预约                 *\n");
                        printf("*******************************************\n");
                        printf("可预约日期：");
                        // 获取当日时间
                        time_t now = 0;
                        time(&now);
                        struct tm to_tm = *localtime(&now);
                        // 输出可预约的日期
                        to_tm.tm_mday += 1;
                        mktime(&to_tm);
                        printf("%d年%d月%d日--", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                        to_tm.tm_mday += 6;
                        mktime(&to_tm);
                        printf("%d年%d月%d日\n", to_tm.tm_year + 1900, to_tm.tm_mon + 1, to_tm.tm_mday);
                        // 选择日期
                        printf("请输入年份: ");
                        scanf_s("%d", &res_year);
                        clear_input_buffer3();
                        printf("请输入月份: ");
                        scanf_s("%d", &res_month);
                        clear_input_buffer3();
                        printf("请输入日期: ");
                        scanf_s("%d", &res_day);
                        clear_input_buffer3();
                        //场地人流输出
                        if (print_time_list(*facility, res_year, res_month, res_day)) {
                            break;
                        }
                    }
                    while (true) {
                        printf("请输入日期信息(示例：2021年03月18日08:00-10:00)\n> ");
                        fgets(input, sizeof(input), stdin);
                        input[strcspn(input, "\n")] = 0;
                        if (parse_time_string(input, res_node)) {
                            char start_hour[6], end_hour[6];
                            strncpy_s(start_hour, 6, res_node->user_res.start_time, 5);
                            start_hour[5] = '\0';
                            strncpy_s(end_hour, 6, res_node->user_res.end_time, 5);
                            end_hour[5] = '\0';
                            if (judge_time(sys, start_hour, end_hour, res_node->user_res.num, res_node->user_res.year, res_node->user_res.month, res_node->user_res.day)) {
                                strncpy_s(res_node->user_res.name, 50, cur_user_node->user.name, 50);
                                strncpy_s(res_node->user_res.id_name, 50, cur_user_node->user.id_name, 50);
                                strncpy_s(res_node->user_res.phone, 50, cur_user_node->user.phone, 50);
                                res_node->user_res.res_count = sys->reservation_link.id + 1;
                                append_res_list(&sys->reservation_link, res_node);
                                save_res_data(&sys->reservation_link);
                                printf("信息已成功保存\n");
                                break;
                            }
                            else {
                                continue;
                            }
                        }
                        else {
                            printf("输入格式错误！请按示例格式输入！\n");
                        }
                    }
                    break;
                }
                else {
                    printf("当前输入的场地编号有误，请重新输入！\n");
                    continue;
                }



            }
        }
    }
}

//查找当前账号下的预订信息
//对场地信息查找
void search_res_facility(System *sys,User_node *cur_user_node) {
    int search_facility = 0;
    int cnt = 0;
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("*          请输入需要查询的场地           *\n");
    printf("*                1,篮球场                 *\n");
    printf("*                2,羽毛球场               *\n");
    printf("*                3,足球场                 *\n");
    printf("*                4,台球场                 *\n");
    printf("*                5,乒乓球场               *\n");
    printf("*                6,排球场                 *\n");
    printf("*******************************************\n");
    scanf_s("%d", &search_facility);
    Res_node* cur_node = sys->reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.phone,cur_user_node->user.phone)==0&&cur_node->user_res.venue_Type==search_facility) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}
//查找场地使用情况
void search_res_flag(System *sys, User_node* cur_user_node) {
    int cnt = 0;
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("*           您已使用过的预约信息          *\n");
    Res_node* cur_node = sys->reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.phone, cur_user_node->user.phone)==0 && cur_node->user_res.flag==1) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}
//查找预约时间情况
void search_res_time(System* sys, User_node* cur_user_node) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    int cnt = 0;
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    Res_node* cur_node = sys->reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.year, year) == 0 &&
            strcmp(cur_node->user_res.month, month) == 0 &&
            strcmp(cur_node->user_res.day, day) == 0&&
            strcmp(cur_node->user_res.phone, cur_user_node->user.phone)==0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条预订信息\n", cnt);
}

// 排序功能
// 实现比较日期
int compare_date(char y1[5], char m1[3], char d1[3],
    char y2[5], char m2[3], char d2[3]) {
    int date1 = atoi(y1) * 10000 + atoi(m1) * 100 + atoi(d1);
    int date2 = atoi(y2) * 10000 + atoi(m2) * 100 + atoi(d2);
    return date1 - date2;
}
// 查看预订信息 Order by 日期
void sort_res_order_by_date(Res_list* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else {
        bool swapped = true;
        Res_node* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head;
            while (ptr->next != NULL) {
                User_res cur = ptr->user_res;
                User_res next = ptr->next->user_res;
                if (compare_date(cur.year, cur.month, cur.day,
                    next.year, next.month, next.day) > 0) {
                    ptr->user_res = next;
                    ptr->next->user_res = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看预订信息 Order by 编号
void sort_res_order_by_id(Res_list* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else {
        bool swapped = true;
        Res_node* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head;
            while (ptr->next != NULL) {
                User_res cur = ptr->user_res;
                User_res next = ptr->next->user_res;
                if (cur.res_count - next.res_count > 0) {
                    ptr->user_res = next;
                    ptr->next->user_res = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看预订信息 Order by 场地编号
void sort_res_order_by_facility_id(Res_list* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else {
        bool swapped = true;
        Res_node* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head;
            while (ptr->next != NULL) {
                User_res cur = ptr->user_res;
                User_res next = ptr->next->user_res;
                if (cur.num - next.num > 0) {
                    ptr->user_res = next;
                    ptr->next->user_res = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
// 查看预订信息 Order by 用户名称
void sort_res_order_by_name(Res_list* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else {
        bool swapped = true;
        Res_node* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head;
            while (ptr->next != NULL) {
                User_res cur = ptr->user_res;
                User_res next = ptr->next->user_res;
                if (strcmp(cur.name,next.name) > 0) {
                    ptr->user_res = next;
                    ptr->next->user_res = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}


//排序界面
void sort_cur_res(System* sys, User_node* cur_user_node) {
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("*           请选择您想排序的字段          *\n");
    printf("*                1,预约日期               *\n");
    printf("*                2,场地编号               *\n");
    printf("*                3,场地类型               *\n");
    printf("*                4,场地面积               *\n");
    printf("*                5,场地费用               *\n");
    printf("*******************************************\n");
    int arr2[6];
    while (true) {
        printf("请输入组合数目：");
        scanf_s("%d", &arr2[0]);
        clear_input_buffer();
        if (arr2[0] < 1 && arr2[0]>5) {
            printf("输入不合法\n");
        }
        else {
            break;
        }
    }
    for (int i = 0; i < arr2[0]; i++) {
        while (true) {
            printf("请依次输入组合：");
            scanf_s("%d", &arr2[i + 1]);
            clear_input_buffer();
            if (arr2[i + 1] >= 1 && arr2[i + 1] <= 5) {
                break;
            }
            else {
                printf("输入不合法\n");
            }
        }
    }
    for (int i = 0; i < arr2[0]; i++) {
        switch (arr2[arr2[0] - i]) {
        case 1:
            sort_res_order_by_date(&sys->reservation_link);
            printf("排序后的预约信息为:\n");
            printf_cur_res(sys->reservation_link, cur_user_node);
            break;
        case 2:
            sort_facility_order_by_id(&sys->facility_link);
            break;
        case 3:
            sort_facility_order_by_type(&sys->facility_link);
            break;
        case 4:
            sort_facility_order_by_area(&sys->facility_link);
        case 5:
            sort_facility_order_by_fee(&sys->facility_link);
            break;
        }

    }
    system("pause");
    system("cls");
    printf("*******************************************\n");
    printf("*           欢迎使用运动场馆系统          *\n");
    printf("*******************************************\n");
    printf("*           请选择您想排序的字段          *\n");
    printf("*                1,预约日期               *\n");
    printf("*                2,场地编号               *\n");
    printf("*                3,场地类型               *\n");
    printf("*                4,场地面积               *\n");
    printf("*                5,场地费用               *\n");
    printf("*******************************************\n");
    printf("排序后的场地信息为:\n");
    print_facilities(&sys->facility_link);

    
}

// 统计功能
// 预约信息总统计
void res_all_statistic(System sys) {
    int cnt = 0;
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        cnt++;
        print_res(cur_node);
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
// 未到场预约统计
void res_default_statistic(System sys) {
    int cnt = 0;
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.flag == 0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
// 统计预约信息   by user
void statistic_res_by_user(System sys) {
    char search_phone[50] = { '\0' };
    int cnt = 0;
    printf(" 输入要查询的电话：");
    scanf_s("%s", search_phone, 50);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (strcmp(cur_node->user_res.phone, search_phone) == 0) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
// 统计预约信息   by facility_type
void statistic_res_by_facility_type(System sys) {
    int type;
    int cnt = 0;
    printf(" 篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入场地类型：");
    scanf_s("%d",&type);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (facility.type == type) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
// 统计预约信息  by user and facility_type
void statistic_res_by_user_and_facility_type(System sys) {
    int type;
    int cnt = 0;
    printf("篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入场地类型：");
    scanf_s("%d", &type);
    clear_input_buffer();
    char search_phone[50] = { '\0' };
    printf(" 输入要查询的电话：");
    scanf_s("%s", search_phone, 50);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (facility.type == type && strcmp(cur_node->user_res.phone, search_phone) == 0) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
// 统计预约信息  日期范围
void statistic_res_by_date_range(System sys) {
    char year1[5] = { '\0' };
    char month1[3] = { '\0' };
    char day1[3] = { '\0' };
    char year2[5] = { '\0' };
    char month2[3] = { '\0' };
    char day2[3] = { '\0' };
    int cnt = 0;
    printf(" 请输入最早日期（格式:YYYY年MM月DD日）:");
    int result1 = scanf_s("%4s年%2s月%2s日", year1, 5, month1, 3, day1, 3);
    clear_input_buffer();
    printf(" 请输入最晚日期（格式:YYYY年MM月DD日）:");
    int result2 = scanf_s("%4s年%2s月%2s日", year2, 5, month2, 3, day2, 3);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                year1, month1, day1) >= 0&&
                compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                    year2, month2, day2) <= 0) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}

//统计当前用户的预订消息的各字段
void statistic_cur_res(System sys, User_node* cur_user_node) {
    //先输出一个预设的统计，然后进行选择
    //日期，使用的不同场地，场地使用次数
    //预设的统计输出所有自己的预订
    //多属性统计（场地类型+日期）（未使用的场地+日期）
    printf("当前的用户所有预约为：\n");
    printf_cur_res(sys.reservation_link, cur_user_node);
    int flag_static = 1;
    while (flag_static) {
        system("pause");
        system("cls");
        printf("*******************************************\n");
        printf("*           欢迎使用运动场馆系统          *\n");
        printf("*******************************************\n");
        printf("*           请选择您想统计的字段          *\n");
        printf("*                1,预约日期               *\n");
        printf("*                2,场地使用次数           *\n");
        printf("*                3,场地类型               *\n");
        printf("*                4,多属性统计             *\n");
        printf("*                0,返回                   *\n");
        printf("*******************************************\n");
        int select = -1;
        while (true) {
            printf("select-> ");
            scanf_s("%d", &select);
            clear_input_buffer();
            if (select != 1 && select != 2 && select != 3 && select != 4 && select != 0) {
                printf("输入有误，请重新输入！\n");
            }
            else if (select == 0) {
                flag_static = 0;
                break;
            }
            else {
                break;
            }
        }
        switch (select) {
        case 1://预约日期
            count_cur_res_data(sys, cur_user_node);
            break;
        case 2://场地使用次数
            count_cur_res_use(sys, cur_user_node);
            break;
        case 3://场地类型
            count_cur_res_type(sys, cur_user_node);
            break;
        case 4://多属性统计
            count_cur_res_properties(sys, cur_user_node);
            break;
        case 0:
            break;
        }
    }
    
    
}

//按日期统计
void count_cur_res_data(System sys, User_node* cur_user_node) {
    char year1[5] = { '\0' };
    char month1[3] = { '\0' };
    char day1[3] = { '\0' };
    char year2[5] = { '\0' };
    char month2[3] = { '\0' };
    char day2[3] = { '\0' };
    int cnt = 0;
    printf(" 请输入最早日期（格式:YYYY年MM月DD日）:");
    int result1 = scanf_s("%4s年%2s月%2s日", year1, 5, month1, 3, day1, 3);
    clear_input_buffer();
    printf(" 请输入最晚日期（格式:YYYY年MM月DD日）:");
    int result2 = scanf_s("%4s年%2s月%2s日", year2, 5, month2, 3, day2, 3);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                year1, month1, day1) >= 0 &&
                compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                    year2, month2, day2) <= 0&&
                !strcmp(cur_node->user_res.phone,cur_user_node->user.phone)) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
//场地使用次数
void count_cur_res_use(System sys, User_node* cur_user_node) {
    int cnt = 0;
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.flag == 1&& 
            !strcmp(cur_node->user_res.phone, cur_user_node->user.phone)) {
            cnt++;
            print_res(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
//场地使用类型统计
void count_cur_res_type(System sys, User_node* cur_user_node) {
    int type;
    int cnt = 0;
    printf(" 篮球场-1 羽毛球场-2 足球场-3 台球场-4 乒乓球场-5 排球场-6\n");
    printf(" 请输入场地类型：");
    scanf_s("%d", &type);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (facility.type == type&&
                !strcmp(cur_node->user_res.phone, cur_user_node->user.phone)) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}
//某短时间内的未使用场地数
void count_cur_res_properties(System sys, User_node* cur_user_node) {
    char year1[5] = { '\0' };
    char month1[3] = { '\0' };
    char day1[3] = { '\0' };
    char year2[5] = { '\0' };
    char month2[3] = { '\0' };
    char day2[3] = { '\0' };
    int cnt = 0;
    printf(" 请输入最早日期（格式:YYYY年MM月DD日）:");
    int result1 = scanf_s("%4s年%2s月%2s日", year1, 5, month1, 3, day1, 3);
    clear_input_buffer();
    printf(" 请输入最晚日期（格式:YYYY年MM月DD日）:");
    int result2 = scanf_s("%4s年%2s月%2s日", year2, 5, month2, 3, day2, 3);
    clear_input_buffer();
    Res_node* cur_node = sys.reservation_link.head;
    while (cur_node != NULL) {
        if (cur_node->user_res.num != 0) {
            Facility facility = *find_facility(&sys.facility_link, cur_node->user_res.num);
            if (compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                year1, month1, day1) >= 0 &&
                compare_date(cur_node->user_res.year, cur_node->user_res.month, cur_node->user_res.day,
                    year2, month2, day2) <= 0 &&
                !strcmp(cur_node->user_res.phone, cur_user_node->user.phone)&&
                cur_node->user_res.flag == 0) {
                cnt++;
                print_res(cur_node);
            }
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息\n", cnt);
}